package com.cloud.wms.ui.inside.presenter;

import android.text.TextUtils;

import com.annimon.stream.Stream;
import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.http.error.ApiException;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.entity.ContainerEntity;
import com.cloud.wms.entity.Page;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.PageHelper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.ReplenishDownContract;
import com.cloud.wms.ui.inside.contract.ReplenishTaskDetailContract;
import com.cloud.wms.ui.inside.entity.ReplenishBatchEntity;
import com.cloud.wms.ui.inside.entity.ReplenishBindEntity;
import com.cloud.wms.ui.inside.entity.ReplenishCommodityEntity;
import com.cloud.wms.ui.inside.entity.ReplenishOrderEntity;
import com.cloud.wms.ui.main.entity.BatchEntity;
import com.cloud.wms.ui.main.entity.RemoteEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.entity.UserEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/3/12.
 * Desc：补货下架
 */
public class ReplenishTaskDetailPresenter extends BaseRxPresenter<ReplenishTaskDetailContract.View> implements ReplenishTaskDetailContract.Presenter {


    public static final int REPLENISH_DETAIL = 0x105;


    @Inject
    public ReplenishTaskDetailPresenter() {

    }

    @Override
    public void loadReplenishOrderDetail(String replenishCode) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        if (!TextUtils.isEmpty(replenishCode)) {
            params.put("ReplenishCode", replenishCode);
        }
        params.put("Sorting", "createDate");
        params.put("SkipCount", 0);
        params.put("MaxResultCount", 1000);
        FineExApplication
                .component()
                .http()
                .loadReplenishOrderList(params)
                .map(new ResponseWrapper<>())
                .flatMap((Function<ArrayList<ReplenishOrderEntity>, ObservableSource<ReplenishOrderEntity>>) replenishOrderEntities -> {
                    if (replenishOrderEntities == null || replenishOrderEntities.isEmpty()) {
                        return Observable.error(new ApiException("未查询到相应补货单！"));
                    }
                    return Observable.just(replenishOrderEntities.get(0));
                })
                .flatMap((Function<ReplenishOrderEntity, ObservableSource<ReplenishOrderEntity>>) replenishOrderEntity -> {
                    Params params1 = Params.newInstance(Params.Type.WAREHOUSE);
                    params1.put("BillID", replenishOrderEntity.getReplenishID());
                    params1.put("QueryType", 1);
                    return
                            FineExApplication.component().http()
                                    .loadReplenishDownCommodity(params1)
                                    .map(new ResponseWrapper<>())
                                    .map(commodityEntities -> {
                                        replenishOrderEntity.setCommodityEntities(commodityEntities);
                                        return replenishOrderEntity;
                                    });
                })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ReplenishOrderEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ReplenishOrderEntity info) {
                        mView.showContent();
                        mView.onSuccess(MessageCreator.createMessage(info, REPLENISH_DETAIL));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.showError();
                        mView.error(msg);
                    }
                });
    }
}
